package org.goyo.main.services.pur.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.main.domain.dao.pur.IPaymentDao;
import org.goyo.main.domain.dao.pur.IPurOrderDao;
import org.goyo.main.domain.entity.pur.Payment;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.pur.IPaymentService;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class PaymentServiceImpl extends BaseService implements  IPaymentService{
	@Resource
	private IPaymentDao paymentDao;
	@Resource
	private IPurOrderDao purOrderDao;

	@Override
	public BigDecimal saveOrUpdateInfo(List<Payment> gridList, Object[] uuids)	throws DBException, ServiceException {
		BigDecimal amount = new BigDecimal(0);
		String parentId = null;
		for(Payment payment : gridList){
			if(parentId == null)
				parentId = payment.getParentId();
			paymentDao.saveOrUpdate(payment);
			//删除数据
			if(uuids != null && uuids[0] != null){
				paymentDao.delBatch(Payment.class, "uuid", uuids);
			}
			amount = amount.add(payment.getPaymentAmout());
		}
		purOrderDao.updateSumPayAmount(amount, parentId);
		return amount;
	}

	@Override
	public List<Payment> findByParentId(String parentId) throws DBException,
			ServiceException {
		return paymentDao.findByParentId(parentId);
	}

}
